package com.jpro.module.system.api.permission;

import com.jpro.framework.common.enums.CommonStatusEnum;
import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.tenant.core.context.TenantContextHolder;
import com.jpro.module.system.api.permission.dto.*;
import com.jpro.module.system.cache.CacheService;
import com.jpro.module.system.dal.dataobject.permission.RoleDO;
import com.jpro.module.system.enums.permission.DataScopeEnum;
import com.jpro.module.system.service.permission.PermissionService;
import com.jpro.module.system.service.permission.RoleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import static com.jpro.framework.common.pojo.CommonResult.success;
import static com.jpro.module.system.dal.redis.RedisKeyConstants.ROLE;
import static java.util.Collections.singleton;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class PermissionApiImpl implements PermissionApi {

    @Resource
    private PermissionService permissionService;
    @Resource
    private RoleService roleService;
    @Resource
    private CacheService cacheService;

    @Override
    public CommonResult<Set<Long>> getUserRoleIdListByRoleIds(Collection<Long> roleIds) {
        return success(permissionService.getUserRoleIdListByRoleId(roleIds));
    }

    @Override
    public CommonResult<Boolean> hasAnyPermissions(Long userId, String... permissions) {
        return success(permissionService.hasAnyPermissions(userId, permissions));
    }

    @Override
    public CommonResult<Boolean> hasAnyRoles(Long userId, String... roles) {
        return success(permissionService.hasAnyRoles(userId, roles));
    }

    @Override
    public CommonResult<DeptDataPermissionRespDTO> getDeptDataPermission(Long userId) {
        return success(permissionService.getDeptDataPermission(userId));
    }

    @Override
    public CommonResult<ChannelPermissionRespDTO> getChannelPermission(Long userId) {
        return success(permissionService.getChannelPermission(userId));
    }

    @Override
    public CommonResult<ContentPermissionRespDTO> getContentPermission(Long userId) {
        return success(permissionService.getContentPermission(userId));
    }

    @Override
    public CommonResult<SitePermissionRespDTO> getSitePermission(Long userId) {
        return success(permissionService.getSitePermission(userId));
    }

    @Override
    public CommonResult<Boolean> hasChannelPermission(Long userId, Long channelId) {
        return success(permissionService.hasChannelPermission(userId, channelId));
    }

    @Override
    public CommonResult<Boolean> assignRoleNewChannelScope(ChannelCreateReqVO reqVO) {
        final List<RoleDO> roleDOList = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus()));
        final Long tenantId = TenantContextHolder.getTenantId();
        for (RoleDO roleDO : roleDOList) {
            System.out.println(roleDO.getId() + " " + roleDO.getName());
            String roleCacheKey = ROLE + ":" + tenantId + ":" + roleDO.getId();
            if (DataScopeEnum.CHANNEL_CUSTOM.getScope().equals(roleDO.getChannelDataScope())) {
                //栏目类
                Set<Long> scopeChannelIdList = roleDO.getScopeChannelIdList();
                //所有权限包含父栏目则自动赋予新栏目权限
                //或者是角色有所有栏目权限
                if ((reqVO.getParentChannelId() != null && scopeChannelIdList.contains(reqVO.getParentChannelId()))
                        || roleDO.getNewAllChannelPerm()) {
                    scopeChannelIdList.add(reqVO.getChannelId());
                    permissionService.assignRoleChannelDataScope(roleDO.getId(), DataScopeEnum.CHANNEL_CUSTOM.getScope(), reqVO.getSiteId(), scopeChannelIdList);
                    cacheService.deleteObject(roleCacheKey);
                }
            }
            if (DataScopeEnum.CHANNEL_CUSTOM.getScope().equals(roleDO.getDocDataScope())) {
                //文档类
                Set<Long> scopeDocChannelIdList = roleDO.getScopeDocChannelIdList();
                //所有权限包含父栏目则自动赋予新栏目权限
                //或者是角色有所有栏目权限
                if ((reqVO.getParentChannelId() != null && scopeDocChannelIdList.contains(reqVO.getParentChannelId()))
                        || roleDO.getNewAllChannelPerm()) {
                    scopeDocChannelIdList.add(reqVO.getChannelId());
                    permissionService.assignRoleContentDataScope(roleDO.getId(), DataScopeEnum.CHANNEL_CUSTOM.getScope(), reqVO.getSiteId(), scopeDocChannelIdList);
                    cacheService.deleteObject(roleCacheKey);
                }
            }
        }
        return success(true);
    }
}
